package com.todo.strategy.online.application.service.pipeline.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.enums.EnableTypeEnum;
import com.todo.component.redis.RedisService;
import com.todo.strategy.common.constant.StrategyOnlineKeyBuild;
import com.todo.strategy.online.application.convert.MemoTemplateConvert;
import com.todo.strategy.online.application.entity.bo.MemoTemplateBO;
import com.todo.strategy.online.application.service.pipeline.MemoTemplatePipelineService;
import com.todo.strategy.online.infrastructure.entity.domain.MemoTemplateDO;
import com.todo.strategy.online.infrastructure.repository.MemoTemplateRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author azhebuxing
 * @date 2025/3/1 15:38
 * @description
 */
@Slf4j
@Service
public class MemoTemplatePipelineServiceImpl implements MemoTemplatePipelineService {

    @Resource
    private MemoTemplateRepository memoTemplateRepository;
    @Resource
    private RedisService redisService;

    @Override
    public Boolean insertMemoTemplate(Long businessId, String originData) {
        MemoTemplateDO memoTemplateDO = JSONObject.parseObject(originData, MemoTemplateDO.class);
        Boolean inserted = memoTemplateRepository.insertMemoTemplate(memoTemplateDO);
        if (inserted) {
            String key = StrategyOnlineKeyBuild.memoTemplateKey(memoTemplateDO.getId());
            MemoTemplateBO memoTemplateBO = MemoTemplateConvert.convertToMemoTemplateBO(memoTemplateDO);
            redisService.set(key, JSONObject.toJSONString(memoTemplateBO));
        }
        log.info("MemoTemplatePipelineService#insertMemoTemplate 线下服务同步插入平台模板完成，businessId:【{}】,result：【{}】,originData：【{}】", businessId, inserted, originData);
        return inserted;
    }

    @Override
    public Boolean updateMemoTemplate(Long businessId, String originData) {
        MemoTemplateDO memoTemplateDO = JSONObject.parseObject(originData, MemoTemplateDO.class);
        Boolean updated = memoTemplateRepository.updateMemoTemplate(memoTemplateDO);
        if (updated) {
            String key = StrategyOnlineKeyBuild.memoTemplateKey(memoTemplateDO.getId());
            redisService.del(key);
        }
        MemoTemplateBO operateData = MemoTemplateConvert.convertToMemoTemplateBO(memoTemplateDO);
        if (EnableTypeEnum.ENABLE.equals(operateData.getStatus())) {
            redisService.set(StrategyOnlineKeyBuild.memoTemplateKey(memoTemplateDO.getId()), JSONObject.toJSONString(operateData));
        }
        if (EnableTypeEnum.FORBIDDEN.equals(operateData.getStatus())) {
            //当前没有操作
        }
        log.info("MemoTemplatePipelineService#updateMemoTemplate 线下服务同步更新平台模板完成，businessId:【{}】,result：【{}】,originData：【{}】", businessId, updated, originData);
        return updated;
    }

    @Override
    public Boolean remoteMemoTemplate(Long businessId) {
        Boolean updated = memoTemplateRepository.removeMemoTemplateDOById(businessId);
        if (updated) {
            String key = StrategyOnlineKeyBuild.memoTemplateKey(businessId);
            redisService.del(key);
        }
        log.info("MemoTemplatePipelineService#remoteMemoTemplate 线下服务同步删除平台模板完成，businessId:【{}】,result：【{}】", businessId, updated);
        return updated;
    }
}
